package com.nfit.seckilldemo1.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;


/**
 * ClassName:RabbitMQConfig
 * Package:com.nfit.seckilldemo1.config
 * Description
 *
 * @Author L
 * @Create 2025/2/17 19:35
 */
@Configuration
public class RabbitMQConfig {
    public static final String ORDER_QUEUE = "seckill_order_queue";
    public static final String ORDER_EXCHANGE = "seckill_order_exchange";
    public static final String ORDER_ROUTING_KEY = "order.create";

    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(ORDER_EXCHANGE, true, false); // 持久化交换机
    }

    @Bean
    public Queue orderQueue() {
        return new Queue(ORDER_QUEUE, true, false, false,
                Collections.singletonMap("x-queue-mode", "lazy")); // 队列消息磁盘存储
    }

    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue())
                .to(orderExchange())
                .with(ORDER_ROUTING_KEY);
    }

    // 配置JSON序列化（需引入Jackson依赖）
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}


